#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>

/*比对的字符串
 *要找的字符串
 *返回要找的字符串在比对的字符串里的起始下标
 */
int nameSearch(char *, char *); //字符串匹配算法

/*被复制文件的路径
 *输出的地址
 */
int fileCopy(char *, char *); //文件拷贝函数

/*关键词
 *抽取文件夹
 *输出文件夹
 */
int fileFindCopy(char *, char *, char *); //抽取文件

void fileFind_Input(); //输入接口

int main(void)
{

    fileFind_Input();
    // fileFindCopy(".c", A, B);
    return 0;
}

void fileFind_Input()
{
    char keyWord[128];

    scanf("%s", keyWord);

    fileFindCopy(keyWord, "inPut", "outPut");
}

int fileFindCopy(char *keyWord, char *serDir, char *destDir)
{
    char dirFile[1024];    //查找文件路径
    char serFolder[1024];  //查找文件夹路径
    char destFolder[1024]; //放置文件的文件夹路径

    int Folder_Flag = 0;         //文件夹的状态
    intptr_t handle;             //文件句柄
    struct _finddata_t fileFind; //查询文件的信息结构体

    memset(dirFile, '\0', 1024);
    memset(serFolder, '\0', 1024);
    memset(destFolder, '\0', 1024);

    strcat(dirFile, serDir);
    strcat(dirFile, "\\");
    strcat(dirFile, "*.*"); //查询全部文件

    if ((handle = _findfirst(dirFile, &fileFind)) != -1)
    {
        do
        {
            strcpy(serFolder, serDir);
            strcpy(destFolder, destDir);

            if (!strcmp(fileFind.name, "..") || !strcmp(fileFind.name, "."))
                continue;

            if (fileFind.attrib == _A_SUBDIR) //遇到文件夹就调用自己,递归
            {
                strcat(destFolder, "\\");
                strcat(destFolder, fileFind.name);
                _mkdir(destFolder);

                strcat(serFolder, "\\");
                strcat(serFolder, fileFind.name);

                fileFindCopy(keyWord, serFolder, destFolder);
            }
            else
            {
                if (nameSearch(fileFind.name, keyWord)) //检索到有关键字的文件
                {
                    strcat(destFolder, "\\");
                    strcat(destFolder, fileFind.name);
                    strcat(serFolder, "\\");
                    strcat(serFolder, fileFind.name);

                    fileCopy(serFolder, destFolder); //将文件复制到输出文件夹
                    Folder_Flag = 1;                 //将文件夹状态置为非空
                }
            }
        } while (!(_findnext(handle, &fileFind)));
        _findclose(handle);

        if ((!Folder_Flag) && (strcmp(destDir, "outPut"))) //若输出文件夹为空状态的文件夹
        {
            _rmdir(destDir); //删除传入的输出文件夹路径
        }
        return 1;
    }
    return 0;
}

int fileCopy(char *inPath, char *outPath)
{
    FILE *inFile = NULL;  //输入文件
    FILE *outFile = NULL; //输出文件
    char strTemp[1024];   //中转字符串

    if (inPath != NULL && outPath != NULL)
    {
        inFile = fopen(inPath, "rb");   //只读二进制打开
        outFile = fopen(outPath, "wb"); //覆写二进制打开

        if ((inFile != NULL) && (outFile != NULL))
            while (fread(strTemp, sizeof(strTemp), 1, inFile))
                fwrite(strTemp, sizeof(strTemp), 1, outFile);

        fclose(inFile);
        fclose(outFile);
        inPath = NULL;
        outPath = NULL;
        return 1;
    }
    return 0;
}

int nameSearch(char *ser, char *dest)
{
    int i = 0, j = 0;
    int serLen = 0, destLen = 0;

    serLen = strlen(ser);
    destLen = strlen(dest);

    while (i < serLen && j < destLen)
        if (ser[i] == dest[j])
        {
            i++;
            j++;
        }
        else
        {
            i = i - j + 1;
            j = 0;
        }
    if (j >= destLen)
        return i - j;
    else
        return 0;
}